import pytest
import requests


@pytest.fixture
def base_url():
    return "http://127.0.0.1:8080"


def test_successful_registration(base_url):
    user_data = {
        "firstName": "John",
        "lastName": "Doe",
        "email": "john.doe@example.com",
        "password": "securepassword",
    }

    response = requests.post(f"{base_url}/registration", json=user_data)

    assert response.status_code == 201
    assert "id" in response.json()
    assert response.json()["firstName"] == user_data["firstName"]
    assert response.json()["lastName"] == user_data["lastName"]
    assert response.json()["email"] == user_data["email"]


@pytest.mark.parametrize(
    "invalid_data",
    [
        {
            "firstName": "",
            "lastName": "Doe",
            "email": "john.doe@example.com",
            "password": "securepassword",
        },  # Пустое имя
        {
            "firstName": "John",
            "lastName": "",
            "email": "john.doe@example.com",
            "password": "securepassword",
        },
        # Пустая фамилия
        {
            "firstName": "John",
            "lastName": "Doe",
            "email": "",
            "password": "securepassword",
        },  # Пустой email
        {
            "firstName": "John",
            "lastName": "Doe",
            "email": "invalidemail",
            "password": "securepassword",
        },  # Невалидный email
        {
            "firstName": "John",
            "lastName": "Doe",
            "email": "john.doe@example.com",
            "password": "",
        },  # Пустой пароль
        {
            "firstName": " ",
            "lastName": "Doe",
            "email": "john.doe@example.com",
            "password": "securepassword",
        },
    ],
)
def test_invalid_user_data(base_url, invalid_data):
    response = requests.post(f"{base_url}/registration", json=invalid_data)

    assert response.status_code == 400


def test_registration_from_authenticated_account(base_url):
    cookie = {"id": "1"}
    user_data = {
        "firstName": "John",
        "lastName": "Doe",
        "email": "john.doe@example.com",
        "password": "securepassword",
    }

    response = requests.post(f"{base_url}/registration", json=user_data, cookies=cookie)

    assert response.status_code == 403


def test_existing_user_registration(base_url):
    existing_user_data = {
        "firstName": "John",
        "lastName": "Doe",
        "email": "john.doe@example.com",
        "password": "securepassword",
    }

    response = requests.post(f"{base_url}/registration", json=existing_user_data)

    assert response.status_code == 409


def test_successful_login(base_url):
    login_data = {"email": "john.doe@example.com", "password": "securepassword"}

    response = requests.post(f"{base_url}/login", json=login_data)

    assert response.status_code == 200
    assert "id" in response.json()


@pytest.mark.parametrize(
    "invalid_login_data",
    [
        {"email": "john.doe@example.com", "password": "incorrectpassword"},
        {"email": "invalidemail", "password": "securepassword"},
        {"email": "nonexistentuser@example.com", "password": "securepassword"},
    ],
)
def test_invalid_login(base_url, invalid_login_data):
    response = requests.post(f"{base_url}/login", json=invalid_login_data)

    assert response.status_code == 401


def test_login_from_authenticated_account(base_url):
    cookie = {"id": "1"}
    login_data = {"email": "john.doe@example.com", "password": "securepassword"}

    response = requests.post(f"{base_url}/login", json=login_data, cookies=cookie)

    assert response.status_code == 200
